package com.example.demo.admin.service;/**
 * Created by kete on 2017-10-13.
 */

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.example.demo.admin.business.IpProxyBusiness;
import com.example.demo.admin.sync.service.PushDataApi;
import com.example.demo.common.annotion.DataSource;

import com.example.demo.common.persistence.dao.IpProxyMapper;
import com.example.demo.common.persistence.dao.MemOrgMapper;
import com.example.demo.common.persistence.dao.MemUserMapper;
import com.example.demo.common.persistence.dao.UserMapper;
import com.example.demo.common.persistence.model.IpProxy;

import com.example.demo.common.persistence.model.MemUser;
import com.example.demo.common.persistence.model.User;
import com.example.demo.core.constant.DSEnum;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 作者：kete
 * 创建时间：2017-10-13 17:13
 * 功能描述：
 * 版本：
 */
@RestController
public class TestServiceImpl implements TestService {

    private static Logger log = LoggerFactory.getLogger(TestServiceImpl.class);

    @Autowired
    UserMapper userMapper;

    @Autowired
    IpProxyMapper ipProxyMapper;

    @Autowired
    IpProxyBusiness business;

    @Autowired
    MemUserMapper memUserMapper;

    @Autowired
    MemOrgMapper memOrgMapper;

    @Autowired
    PushDataApi pushDataApi;


    @RequestMapping(value = "/cas/addUser",method = RequestMethod.GET)
    public String addUser(){

//        MemUser user = memUserMapper.selectById(64346);
//        ApiUserInfoVo apiUserInfoVo = new ApiUserInfoVo();
//        BeanUtils.copyProperties(user,apiUserInfoVo);
//        pushDataApi.pushNewUser(apiUserInfoVo);
//        MemOrg memOrg = memOrgMapper.selectById(59015);
//        ApiOrgInfoVo apiOrgInfoVo = new ApiOrgInfoVo();
//        BeanUtils.copyProperties(memOrg,apiOrgInfoVo);
//
//        pushDataApi.pushOldOrg(10);

//        MessageVo msg = new MessageVo();
//        msg.addUserId(64364);
//        msg.addAttribute("userName", "小强");
//        msg.addAttribute("mainName", "hello word");
//        msg.addAttribute("supplierOrgName", "小白");
//        msg.addAttribute("SiteUrl", "www.hao123.com");
//        msg.addAttribute("Url", "www.hao123.com");
//        msg.setBusinessId(99);
//        msg.setBusinessCode("EBS_RM_SIGNUP_TODO");
//        msg.setMsgNodeCode("ebs_supplier_signup_node");
//        sendMessage.send(msg);

//        CacheManager.set("username","xiaohei",60*5);

        business.initCityEn();

        return "add user";

    }

    @DataSource(name = DSEnum.DATA_SOURCE_GUNS)
    @RequestMapping(value = "/user/testGuns",method = RequestMethod.GET)
    public User testGuns(){

        return getUserInfo();
    }

    @DataSource(name = DSEnum.DATA_SOURCE_BIZ)
    @RequestMapping(value = "/user/testGiz",method = RequestMethod.GET)
    public MemUser testGiz(){
        MemUser user = memUserMapper.selectById(1437);
        return user;
    }

    @Override
    @RequestMapping(value = "/user/getUserInfo",method = RequestMethod.GET)
    public User getUserInfo() {

        Wrapper parameter = new EntityWrapper<User>();
        parameter.eq("account","admin");

        List<User> list = userMapper.selectList(parameter);
        User user = new User();
        for(User use:list){
            log.info(use.toString());
            user = use;
        }

        return user;
    }

    @RequestMapping(value = "/test/addProxy",method = RequestMethod.GET)
    public String addProxy(){

        IpProxy ipProxy = new IpProxy();
        ipProxy.setProxyIp("123212321");
        ipProxy.setProxyPort("66");

        ipProxyMapper.insert(ipProxy);

        return "{添加成功}";
    }

    @RequestMapping(value = "/test/getIpProxy",method = RequestMethod.GET)
    public String getIpProxy(){

        business.geccocProxy();

        return "{getting proxy}";
    }


    @RequestMapping(value = "/test/deleteProxy",method = RequestMethod.GET)
    public String deleteProxy(){
        business.deleteProxy();

        return "{deleted proxy}";
    }

    @RequestMapping(value = "/test/refreshId",method = RequestMethod.GET)
    public String refreshId(){

        new Thread("刷战绩"){
            @Override
            public void run() {
                business.refreshId();
            }
        }.start();

        return "{刷完完了}";
    }

    public static void main(String[] args){

//        String json = null;
//        try {
//            URL url = Resources.getResource("city.txt");
//            File file = new File(url.getPath());
//            if(file.exists()) {
//
//                json = Files.toString(file, Charset.forName("UTF-8"));
//
//            }
//        } catch(IllegalArgumentException ex) {
//            log.info("starts.json not found");
//        } catch(Exception ioex) {
//            ioex.printStackTrace();
//        }
//
//        List<String> strList = Arrays.asList(json.split("/r/n"));
        List<String> list = Lists.newArrayList();

        List<String> sb = list.stream().filter(t->t.equals("b")).collect(Collectors.toList());

        sb.forEach(s-> System.out.printf(s));
    }


}
